// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// B [email]  rhymelph@gmail.com
// a [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/role_permission_entity.dart';
import 'package:finance_app/src/provider/invite_team/new_role_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/permission_select_form.dart';
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "newRolePage")
class NewRolePage extends StatefulWidget {
  final RolePermissionEntity data;
  final bool isDuplicate;
  final bool isGoBack;

  const NewRolePage({Key key, this.data, this.isDuplicate, this.isGoBack})
      : super(key: key);

  @override
  _NewRolePageState createState() => _NewRolePageState();
}

class _NewRolePageState extends State<NewRolePage>
    with PagePresenter<NewRolePage, NewRoleProvider> {
  @override
  void initState() {
    provider.initPermission(widget.data, widget.isDuplicate, widget.isGoBack);
    super.initState();
  }

  Widget get rightButton => listBuilder<bool>(
        selector: (_, __) =>
            (__.isEdit == true || provider.rolePermission?.id == null),
        builder: (BuildContext context, bool value, Widget child) =>
            value ? _buildSaveBtn() : _buildPopButton(),
      );

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          titleWidget: listBuilder<Tuple2<bool, bool>>(
            selector: (_, __) =>
                Tuple2(__.rolePermission?.id == null, __.isEdit),
            builder: (BuildContext context, Tuple2<bool, bool> value,
                    Widget child) =>
                Text(value.item1
                    ? S.current.add_new_role
                    : value.item2
                        ? S.current.edit_permission
                        : S.current.permissions),
          ),
          actions: [
            rightButton,
          ]);

  Widget _buildPopButton() => listBuilder<Tuple2<bool, List<String>>>(
        selector: (_, __) => Tuple2(!__.isEdit, List.from(__.menuList)),
        builder: (BuildContext context, Tuple2<bool, List<String>> value,
                Widget child) =>
            value.item1
                ? NormalMorePopupButton(
                    onSelect: provider.onSelectMenu,
                    menuList: provider.menuList,
                  )
                : Container(),
      );

  Widget _buildSaveBtn() {
    return listBuilder<bool>(
      selector: (_, __) => pageStatus != PageStatus.loading,
      builder: (BuildContext context, bool value, Widget child) => value == true
          ? NormalSaveTextButton(
              onTap: provider.onSave,
            )
          : Container(),
    );
  }

  @override
  Widget wrapperBody(Widget child) {
    return listBuilder<bool>(
        selector: (_, __) =>
            __.isEdit == true || provider.rolePermission?.id == null,
        builder: (BuildContext context, bool value, Widget child) =>
            FormEditStatusInheritedWidget(
              isEdit: value,
              child: child,
            ),
        child: child);
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(left: 24.0),
              child: listBuilder<bool>(
                selector: (_, __) => __.isEdit || __.rolePermission.id == null,
                builder: (BuildContext context, bool value, Widget child) =>
                    TextField(
                  controller: provider.nameController,
                  scrollPadding: EdgeInsets.zero,
                  obscureText: false,
                  textCapitalization: TextCapitalization.words,
                  enabled: value,
                  style: TextStyle(
                    fontSize: getSp(16),
                    color: value ? aceBlueColor : aceBlackColor,
                    fontWeight: FontWeight.bold,
                  ),
                  decoration: InputDecoration(
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      disabledBorder: InputBorder.none,
                      hintText: S.current.role_name,
                      hintStyle: TextStyle(
                        color: aceGrayColor,
                        fontWeight: FontWeight.w700,
                        fontSize: getSp(16),
                      )),
                ),
              ),
            ),
            Container(
              height: getDp(32),
              color: aceWhiteColor,
              padding: EdgeInsets.symmetric(horizontal: getDp(24)),
              alignment: Alignment.centerLeft,
              child: Text(
                S.current.permissions,
                style: TextStyle(
                  color: aceDarkGrayColor,
                  fontSize: getSp(16),
                  fontWeight: FontWeight.bold,
                  height: 22 / 16,
                ),
              ),
            ),
            Expanded(
              child: ListView(
                children: [
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.homeDashboard,
                        __.isEdit || __.rolePermission.id == null,
                        __.homeDashboard),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.quotation,
                        __.isEdit || __.rolePermission.id == null,
                        __.quotation),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.invoice,
                        __.isEdit || __.rolePermission.id == null,
                        __.invoice),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.purchaseOrder,
                        __.isEdit || __.rolePermission.id == null,
                        __.purchaseOrder),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.bill,
                        __.isEdit || __.rolePermission.id == null,
                        __.bill),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.expense,
                        __.isEdit || __.rolePermission.id == null,
                        __.expense),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.creditDebitNote,
                        __.isEdit || __.rolePermission.id == null,
                        __.creditDebitNote),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.productService,
                        __.isEdit || __.rolePermission.id == null,
                        __.productService),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.businessContact,
                        __.isEdit || __.rolePermission.id == null,
                        __.businessContact),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.cashBank,
                        __.isEdit || __.rolePermission.id == null,
                        __.cashBank),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.hrPayroll,
                        __.isEdit || __.rolePermission.id == null,
                        __.hrPayroll),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(
                        horizontal: getDp(24), vertical: 10),
                    child: Text(
                      S.current.all_reports,
                      style: TextStyle(
                          fontSize: 15,
                          color: aceBlackColor,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.salesReport,
                        __.isEdit || __.rolePermission.id == null,
                        __.salesReport),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.purchaseReports,
                        __.isEdit || __.rolePermission.id == null,
                        __.purchaseReports),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.inventoryReports,
                        __.isEdit || __.rolePermission.id == null,
                        __.inventoryReports),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.taxesReports,
                        __.isEdit || __.rolePermission.id == null,
                        __.taxesReports),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.hrPayrollReports,
                        __.isEdit || __.rolePermission.id == null,
                        __.hrPayrollReports),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.accountantReports,
                        __.isEdit || __.rolePermission.id == null,
                        __.accountantReports),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                      paddingLeft: true,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.chartOfAccounts,
                        __.isEdit || __.rolePermission.id == null,
                        __.chartOfAccounts),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.companySettings,
                        __.isEdit || __.rolePermission.id == null,
                        __.companySettings),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  listBuilder<Tuple3<String, bool, bool>>(
                    selector: (_, __) => Tuple3<String, bool, bool>(
                        PermissionUtils.inviteTeamMembers,
                        __.isEdit || __.rolePermission.id == null,
                        __.inviteTeamMembers),
                    builder: (BuildContext context,
                            Tuple3<String, bool, bool> value, Widget child) =>
                        PermissionSelectForm(
                      title: provider.getPermissionName(value.item1),
                      onTap: () => provider.onChangePermission(value.item1),
                      isEnable: value.item2,
                      isSelect: value.item3,
                    ),
                  ),
                  AppSpaces.h50,
                ],
              ),
            ),
          ],
        ),
      );
}
